package com.sywb.chuangyebao.view;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.CategoryBean;
import com.sywb.chuangyebao.contract.SearchJoinContract;

import org.bining.footstone.widget.DrawableEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pxb on 2018/6/11.
 */

public class SearchJoinActivity extends ActionbarActivity<SearchJoinContract.SearchJoinPresenter> implements SearchJoinContract.SearchJoinView {
    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;

    @BindView(R.id.industry_tv)
    TextView industryTv;

    @BindView(R.id.address_tv)
    TextView addressTv;

    @BindView(R.id.rl_address)
    RelativeLayout rlAddress;

    @BindView(R.id.rl_join_industry)
    RelativeLayout rlIndustry;

    @BindView(R.id.common_recycler_address)
    RecyclerView addressRecyclerView;

    @BindView(R.id.address_ll)
    LinearLayout addressLl;

    @BindView(R.id.common_recycler_industry)
    RecyclerView industryRecycler;

    @BindView(R.id.common_recycler_industry_two)
    RecyclerView industryRecyclerTwo;

    @BindView(R.id.industry_ll)
    LinearLayout industryLl;

    @BindView(R.id.srarch_edit)
    DrawableEditText srarchEdit;

    @BindView(R.id.search_button)
    TextView searchButton;

    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;

    private int selectedPosition = -1;

    private Drawable selectedDrawable;

    private Drawable drawable;

    private String mClickID;

    private List<CategoryBean> mDataCategroy;

    private String mClickSecID;

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        selectedDrawable = getResources().getDrawable(R.drawable.option_on);
        selectedDrawable.setBounds(0, 0, selectedDrawable.getMinimumWidth(), selectedDrawable.getMinimumHeight());
        drawable = getResources().getDrawable(R.drawable.option_off);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mClickID = getIntent().getStringExtra("p0");
        mDataCategroy = (List<CategoryBean>) getIntent().getSerializableExtra("p1");
        mClickSecID = getIntent().getStringExtra("p2");
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_search_join;
    }

    @OnClick({R.id.srarch_back, R.id.rl_address, R.id.address_ll, R.id.rl_join_industry})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.srarch_back:
                exit();
                break;
            case R.id.rl_join_industry:
                changeTabState(0);
                break;
            case R.id.rl_address:
                changeTabState(1);
                break;
            case R.id.address_ll:
                changeTabState(selectedPosition);
                break;

        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    /**
     * 设置行业 地址选中 未选中状态
     *
     * @param position
     */
    private void changeTabState(int position) {
        if (selectedPosition == position) {
            selectedPosition = -1;
            //changeTabNormal(position);
            return;
        }
        if (addressLl.getVisibility() != View.VISIBLE) {
            addressLl.setVisibility(View.VISIBLE);
        }

        this.selectedPosition = position;
        switch (position) {
            case 0:
                setAttributes(addressTv);
                setAnimation(selectedPosition);
                break;
            case 1:
                setAttributes(industryTv);
                setAnimation(selectedPosition);
                break;
        }
    }

    private void changeTabNormal(int position) {
        switch (position) {
            case 0:
                setAttributes(industryTv);
                setAnimation(selectedPosition);
                break;
            case 1:
                setAttributes(addressTv);
                setAnimation(selectedPosition);
                break;
        }
    }

    private void setAttributes(TextView view) {
        view.setSelected(false);
        view.setCompoundDrawables(null, null, drawable, null);
        view.setTextColor(ContextCompat.getColor(mContext, R.color.colorDark));
    }


    //设置动画

    private void setAnimation(final int position) {
        Animation alphaAnimation = AnimationUtils.loadAnimation(SearchJoinActivity.this,
                R.anim.dialog_alpha_in);
        Animation alphaAnimationOut = AnimationUtils.loadAnimation(SearchJoinActivity
                .this, R.anim.dialog_alpha_out);
        if (position == 1) {//地址
            addressTv.setSelected(true);
            addressTv.setCompoundDrawables(null, null, selectedDrawable, null);
            addressTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorTheme));

            addressRecyclerView.startAnimation(alphaAnimation);
            addressRecyclerView.setVisibility(View.VISIBLE);
            industryLl.setVisibility(View.GONE);
        } else if (position == 0) {//行业
            industryTv.setSelected(true);
            industryTv.setCompoundDrawables(null, null, selectedDrawable, null);
            industryTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorTheme));

            industryLl.setAnimation(alphaAnimation);
            industryLl.setVisibility(View.VISIBLE);
            addressRecyclerView.setVisibility(View.GONE);
        } else if (position == -1) {
            if (addressRecyclerView.getVisibility() == View.VISIBLE) {
                addressLl.startAnimation(alphaAnimationOut);
                industryLl.setVisibility(View.GONE);
                addressRecyclerView.setVisibility(View.GONE);
                addressLl.setVisibility(View.GONE);
                setAttributes(addressTv);
            } else if (industryLl.getVisibility() == View.VISIBLE) {
                addressLl.startAnimation(alphaAnimationOut);
                addressRecyclerView.setVisibility(View.GONE);
                industryLl.setVisibility(View.GONE);
                addressLl.setVisibility(View.GONE);
                setAttributes(industryTv);
            }
        }
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
        commonNodata.setVisibility(isNoData ? View.VISIBLE : View.GONE);
    }

    @Override
    public TextView clickSearch() {
        return searchButton;
    }

    @Override
    public EditText getSearch() {
        return srarchEdit;
    }

    @Override
    public RecyclerView addressRecycler() {
        return addressRecyclerView;
    }

    @Override
    public RecyclerView industryRecycler() {
        return industryRecycler;
    }

    @Override
    public RecyclerView secondIndustryRecycler() {
        return industryRecyclerTwo;
    }

    @Override
    public String getClickID() {
        return mClickID;
    }

    @Override
    public String getClickSecID() {
        return mClickSecID;
    }

    @Override
    public List<CategoryBean> getDataCategory() {
        return mDataCategroy;
    }

    @Override
    public TextView tvIndustry() {
        return industryTv;
    }

    @Override
    public TextView tvAddress() {
        return addressTv;
    }

    @Override
    public void changePosition(int position) {
        setAnimation(position);
    }

}
